<template>
  <div class="page-container">
    <!-- 顶部导航 -->
    <header class="header">
      <div class="header-container">
        <div class="back-btn" @click="handleBack">
          <i class="fas fa-arrow-left"></i>
        </div>
        <h1 class="header-title">危机咨询记录</h1>

        <div class="header-left">
          <!-- <div class="header-icon">
            <i class="fas fa-headset"></i>
          </div> -->
        </div>
      </div>
    </header>

    <main class="main-content">
      <!-- 主记录卡片 -->
      <div class="card urgent animate-fade-in">
        <div class="card-header">
          <div class="patient-info">
            <div class="patient-icon">
              <i class="fas fa-user-injured"></i>
            </div>
            <h2 class="patient-name">{{ detail.zkName }} - 危机咨询记录</h2>
          </div>
          <span
            class="status-badge"
            :class="getRiskLevelClass(detail.crisisLevel)"
            >{{ detail.crisisLevel }}</span
          >
        </div>

        <!-- 基本信息 -->
        <div class="info-section">
          <div class="section-title">
            <i class="fas fa-info-circle"></i>
            <span>基本信息</span>
          </div>
          <div class="info-content">
            <div class="basic-info">
              <div class="info-row">
                <span class="info-label">档案编号：</span>
                <span class="info-value">{{ detail.code }}</span>
              </div>
              <div class="info-row">
                <span class="info-label">保密情况：</span>
                <span class="info-value">{{ detail.confidentSituation }}</span>
              </div>

              <div class="info-row">
                <span class="info-label">联系电话：</span>
                <span class="info-value">{{
                  detail.callphone || "未提供"
                }}</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 咨询师信息 -->
        <div class="info-section">
          <div class="section-title">
            <i class="fas fa-user-md"></i>
            <span>咨询师</span>
          </div>
          <div class="info-content">
            <div class="consultant-info">
              <div class="consultant-avatar">
                {{ getInitials(detail.ucName) }}
              </div>
              <div class="consultant-details">
                <h3 class="consultant-name">{{ detail.ucName }}</h3>
                <p class="consultant-title">危机干预咨询师</p>
              </div>
            </div>
          </div>
        </div>

        <!-- 咨询时间 -->
        <div class="info-section">
          <div class="section-title">
            <i class="far fa-calendar-alt"></i>
            <span>咨询时间</span>
          </div>
          <div class="info-content">
            <div class="time-details">
              <div class="time-item">
                <p class="time-label">来电时间</p>
                <p class="time-value">{{ formatDateTime(detail.calltime) }}</p>
              </div>
              <div class="time-item">
                <p class="time-label">创建时间</p>
                <p class="time-value">
                  {{ formatDateTime(detail.createdate) }}
                </p>
              </div>
              <div class="time-item">
                <p class="time-label">记录时间</p>
                <p class="time-value">
                  {{ formatDateTime(detail.createTime) }}
                </p>
              </div>
            </div>
          </div>
        </div>

        <!-- 危机情况 -->
        <div class="info-section" v-if="detail.crisiSituation">
          <div class="section-title">
            <i class="fas fa-exclamation-triangle"></i>
            <span>危机情况</span>
          </div>
          <div class="info-content">
            <p>{{ detail.crisiSituation || "暂无危机情况描述" }}</p>
          </div>
        </div>

        <!-- 咨询要点 -->
        <div class="info-section" v-if="detail.consultationPoint">
          <div class="section-title">
            <i class="fas fa-clipboard-list"></i>
            <span>咨询要点</span>
          </div>
          <div class="info-content notes-content">
            <p>{{ detail.consultationPoint || "暂无咨询要点" }}</p>
            <div
              class="critical-info"
              v-if="detail.crisisLevel && detail.crisisLevel !== '低风险'"
            >
              <i class="fas fa-exclamation-circle"></i>
              <span>评估风险等级：{{ detail.crisisLevel }}</span>
            </div>
          </div>
        </div>

        <!-- 观察与建议 -->
        <div class="info-section" v-if="detail.observationSuggestion">
          <div class="section-title">
            <i class="fas fa-binoculars"></i>
            <span>观察与建议</span>
          </div>
          <div class="info-content observation-content">
            <p>{{ detail.observationSuggestion || "暂无观察与建议" }}</p>
          </div>
        </div>

        <!-- 操作按钮 -->
        <!-- <div class="action-buttons">
          <button class="btn-primary" @click="updateRecord">
            <i class="fas fa-file-medical"></i>
            <span>更新记录</span>
          </button>
          <button
            class="btn-secondary"
            @click="printReport"
            :disabled="!detail.reportUrl"
          >
            <i class="fas fa-print"></i>
            <span>{{ detail.reportUrl ? "打印报告" : "暂无报告" }}</span>
          </button>
        </div> -->
      </div>

      <!-- 历史记录 -->
      <h3 class="history-title" v-if="historyList.length > 0">
        <i class="fas fa-history"></i>
        <span>历史咨询记录</span>
      </h3>

      <div class="history-list" v-if="historyList.length > 0">
        <!-- 这里可以后续对接历史记录接口 -->
        <div class="no-history" v-if="!historyList.length">
          <i class="fas fa-file-alt"></i>
          <p>暂无历史咨询记录</p>
        </div>

        <!-- 历史记录卡片 -->
        <div
          v-for="(record, index) in historyList"
          :key="index"
          class="history-card animate-fade-in"
          :style="{ 'animation-delay': `${index * 0.1}s` }"
          @click="loadRecord(record)"
        >
          <div class="history-header">
            <h4 class="history-name">{{ record.title }}</h4>
            <span class="history-date">{{ formatDateTime(record.date) }}</span>
          </div>
          <div class="history-details">
            <div class="history-detail">
              <i class="fas fa-user-md"></i>
              <span>{{ record.consultant }}</span>
            </div>
            <div class="history-detail">
              <i class="fas fa-chart-line"></i>
              <span>{{ record.riskLevel }}</span>
            </div>
          </div>
        </div>
      </div>
    </main>
    <div class="btns">
      <button class="retry-btn2" @click="gohome()">返回首页</button>
    </div>
  </div>
</template>

<script>
import { getDetail } from "../../api/manager";
export default {
  name: "CrisisConsultation",
  data() {
    return {
      activeNav: "records",
      detail: {
        code: "",
        ucName: "",
        crisisLevel: "",
        confidentSituation: "",
        zkName: "",
        memberno: "",
        callphone: "",
        czName: "",
        calltime: "",
        createdate: "",
        consultationPoint: "",
        observationSuggestion: "",
        crisiSituation: "",
        reportUrl: "",
        createTime: "",
      },
      historyList: [], // 历史记录列表
    };
  },
  created() {
    this.id = this.$route.query.id;
    this.getDetail();
  },
  methods: {
    gohome() {
      this.$router.go(-2);
    },
    async getDetail() {
      try {
        const res = await getDetail({ id: this.id });
        if (res.code === 200) {
          this.detail = res.data;
          // 这里可以调用获取历史记录的方法
          // this.getHistoryList();
        } else {
          this.showToast("获取详情失败", "error");
        }
      } catch (error) {
        console.error("获取详情错误:", error);
        this.showToast("网络错误，请重试", "error");
      }
    },

    // 格式化日期时间
    formatDateTime(dateTime) {
      if (!dateTime) return "-";
      return dateTime.replace(" ", " "); // 保持原格式，可以根据需要调整
    },

    // 获取风险等级对应的样式类
    getRiskLevelClass(level) {
      const levelMap = {
        高风险: "status-urgent",
        中风险: "status-medium",
        低风险: "status-low",
        紧急干预: "status-urgent",
      };
      return levelMap[level] || "status-low";
    },

    // 获取姓名的首字母
    getInitials(name) {
      if (!name) return "匿";
      return name.length > 1 ? name.substring(0, 1) : name;
    },

    // 返回上一页
    handleBack() {
      this.$router.go(-1);
    },

    setActiveNav(nav) {
      this.activeNav = nav;
    },

    updateRecord() {
      const button = event.currentTarget;
      const originalHTML = button.innerHTML;
      button.innerHTML = '<i class="fas fa-spinner"></i> 保存中...';
      button.classList.add("loading");

      setTimeout(() => {
        button.innerHTML = originalHTML;
        button.classList.remove("loading");
        this.showToast("记录更新成功！", "success");
      }, 1500);
    },

    printReport() {
      if (this.detail.reportUrl) {
        window.open(this.detail.reportUrl, "_blank");
        this.showToast("正在打开报告...", "loading");
      } else {
        this.showToast("暂无报告可打印", "error");
      }
    },

    loadRecord(record) {
      this.showToast(`正在加载: ${record.title}`, "loading");
      setTimeout(() => {
        this.showToast(`已加载: ${record.title}`, "success");
      }, 1000);
    },

    showToast(message, type) {
      const toast = document.createElement("div");
      toast.className = `toast toast-${type}`;

      let icon = "";
      if (type === "success") {
        icon = '<i class="fas fa-check-circle"></i>';
      } else if (type === "loading") {
        icon = '<i class="fas fa-spinner fa-spin"></i>';
      } else if (type === "error") {
        icon = '<i class="fas fa-exclamation-circle"></i>';
      }

      toast.innerHTML = `${icon} ${message}`;
      document.body.appendChild(toast);

      setTimeout(() => {
        toast.classList.add("show");
      }, 10);

      setTimeout(() => {
        toast.classList.remove("show");
        setTimeout(() => {
          document.body.removeChild(toast);
        }, 300);
      }, 2000);
    },
  },
};
</script>

<style scoped>
/* 在原有样式基础上添加以下样式 */

/* 基本信息样式 */
.basic-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.info-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 0;
}

.info-label {
  color: #6b7280;
  font-size: 14px;
  font-weight: 500;
}

.info-value {
  color: #1e293b;
  font-weight: 600;
}

/* 风险等级样式 */
.status-medium {
  background-color: #fef3c7;
  color: #d97706;
}

.status-low {
  background-color: #d1fae5;
  color: #059669;
}

/* 无历史记录样式 */
.no-history {
  text-align: center;
  padding: 40px 20px;
  color: #9ca3af;
}

.no-history i {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.no-history p {
  font-size: 16px;
  margin: 0;
}

/* 错误状态样式 */
.toast-error {
  background-color: #ef4444;
}

/* 按钮禁用样式 */
.btn-secondary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 咨询要点和观察建议的空状态样式 */
.notes-content p:empty::before,
.observation-content p:empty::before {
  content: "暂无内容";
  color: #9ca3af;
  font-style: italic;
}

/* 时间详情网格布局调整 */
.time-details {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.time-item {
  padding: 8px 0;
}

.time-label {
  font-size: 14px;
  color: #6b7280;
  margin-bottom: 4px;
}

.time-value {
  font-weight: 600;
  color: #4f46e5;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .time-details {
    grid-template-columns: 1fr;
  }

  .info-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  .action-buttons {
    grid-template-columns: 1fr;
  }
}

/* 其他原有样式保持不变 */
.page-container {
  font-family: "Noto Sans SC", sans-serif;
  background-color: #f8fafc;
  color: #1e293b;
  line-height: 1.6;
  padding-bottom: 80px;
  position: relative;
  min-height: 100vh;
}

.header {
  position: sticky;
  top: 0;
  z-index: 50;
  background-color: white;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.header-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0px 50px 0 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-icon {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background-color: #e0e7ff;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #4f46e5;
}

.header-icon i {
  font-size: 20px;
}

.header-title {
  font-size: 20px;
  font-weight: 700;
}

.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
}

.card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
    0 4px 6px -2px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  padding: 24px;
  margin-bottom: 24px;
}

.card:hover {
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1),
    0 10px 10px -5px rgba(0, 0, 0, 0.04);
}

.urgent {
  background-color: #fff5f5;
  border-left: 4px solid #ef4444;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #f3f4f6;
}

.patient-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.patient-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background-color: #e0e7ff;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #4f46e5;
}

.patient-icon i {
  font-size: 20px;
}

.patient-name {
  font-size: 18px;
  font-weight: 700;
}

.status-badge {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 600;
}

.status-urgent {
  background-color: #fee2e2;
  color: #dc2626;
}

.info-section {
  margin-bottom: 24px;
}

.section-title {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 500;
  color: #4b5563;
}

.section-title i {
  margin-right: 8px;
  color: #4f46e5;
}

.info-content {
  background-color: #f9fafb;
  border-radius: 12px;
  padding: 16px;
}

.consultant-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.consultant-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 700;
  font-size: 18px;
}

.consultant-name {
  font-weight: 700;
  margin-bottom: 4px;
}

.consultant-title {
  font-size: 14px;
  color: #6b7280;
}

.notes-content {
  min-height: 120px;
}

.critical-info {
  background-color: #fef2f2;
  border-radius: 8px;
  padding: 12px;
  display: flex;
  align-items: flex-start;
  margin-top: 12px;
}

.critical-info i {
  color: #ef4444;
  margin-right: 8px;
  margin-top: 2px;
}

.critical-info span {
  color: #dc2626;
  font-weight: 500;
}

.observation-content {
  background-color: #fdf2f8;
  min-height: 150px;
}

.action-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.btn-primary,
.btn-secondary {
  padding: 12px 16px;
  border-radius: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  cursor: pointer;
  transition: opacity 0.3s;
}

.btn-primary {
  background: linear-gradient(to right, #6366f1, #8b5cf6);
  color: white;
}

.btn-secondary {
  background-color: white;
  border: 1px solid #e5e7eb;
  color: #374151;
}

.btn-primary.loading {
  opacity: 0.75;
}

.history-title {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
}

.history-title i {
  color: #4f46e5;
  margin-right: 8px;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.history-card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
    0 4px 6px -2px rgba(0, 0, 0, 0.05);
  padding: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.history-card:hover {
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1),
    0 10px 10px -5px rgba(0, 0, 0, 0.04);
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.history-name {
  font-weight: 700;
}

.history-date {
  font-size: 14px;
  color: #6b7280;
}

.history-details {
  display: flex;
  gap: 16px;
  font-size: 14px;
  color: #6b7280;
}

.history-detail {
  display: flex;
  align-items: center;
}

.history-detail i {
  color: #4f46e5;
  margin-right: 4px;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.animate-fade-in {
  animation: fadeIn 0.5s ease-out forwards;
}

.toast {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(-20px);
  background-color: #10b981;
  color: white;
  padding: 8px 16px;
  border-radius: 9999px;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  opacity: 0;
  transition: all 0.3s ease;
  z-index: 1000;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast i {
  margin-right: 8px;
}

.toast-loading {
  background-color: #6366f1;
}

.toast-success {
  background-color: #10b981;
}
.back-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #eef0ff;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #5b67ff;
  cursor: pointer;
  transition: all 0.3s ease;
  touch-action: manipulation; /* 改善触摸响应 */
}

.btns {
  display: flex;
  justify-content: space-around;
}
.retry-btn2 {
  background: #40a2f4;

  color: white;
  border: none;
  padding: 12px 30px;
  border-radius: 25px;
  cursor: pointer;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}
</style>
